home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / lookup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-31  |  3.5 KB  |  102 lines

  1. // lookup.cpp
  2. //
  3. // Example program uses the string class and the btree dictionary class.
  4. // Compendium class names and descriptions of these classes are inserted
  5. // as key/value pairs into the dictionary.  If the program is run with no
  6. // command line arguments, the entires contents of the dictionary are
  7. // printed to the screen.  If a command line argument is typed, it is
  8. // assumed to be a class name.  A "lookup" is performed to find the
  9. // equivalent class name in the dictionary and, if found, that class
  10. // name and it's description is printed to the screen.
  11.  
  12. #include <cm/include/cmbdict.h>
  13. #include <cm/include/cmstring.h>
  14.  
  15. // Prototype print function.
  16. void printDictionary(const CmBTreeDictionary&);
  17.  
  18. const int TOTAL_CLASSES = 23;                   // Define number of strings.
  19.  
  20. char *classnames[TOTAL_CLASSES] =               // Class name strings.
  21. { "CmArray",
  22.   "CmAssociation",
  23.   "CmBag",
  24.   "CmBTreeDictionary",
  25.   "CmBinaryTree",
  26.   "CmBTree",
  27.   "CmContainer",
  28.   "CmDate",
  29.   "CmDeque",
  30.   "CmHashTable",
  31.   "CmHashDictionary",
  32.   "CmLinkedList",
  33.   "CmObject",
  34.   "CmOrderedArray",
  35.   "CmQueue",
  36.   "CmReserve",
  37.   "CmReserveFile",
  38.   "CmRing",
  39.   "CmSet",
  40.   "CmStack",
  41.   "CmString",
  42.   "CmTime",
  43.   "CmTokens" };
  44.  
  45. char *descriptions[TOTAL_CLASSES] =             // Class description strings.
  46. { "One dimensional array class.",
  47.   "Key/data class used for dictionaries.",
  48.   "Bag class implemented as a hash table.",
  49.   "Balanced tree of associations.",
  50.   "Binary tree class.",
  51.   "Balanced tree class.",
  52.   "Base class for all object based containers.",
  53.   "Date class.",
  54.   "Double ended queue class.",
  55.   "Hash table class.",
  56.   "Hash table of associations.",
  57.   "Singly linked list class.",
  58.   "Abstract base class for all objects.",
  59.   "One dimensional sorted array class.",
  60.   "Queue class implemented as linked list.",
  61.   "File I/O management class.",
  62.   "Binary file interface class.",
  63.   "Circularly linked list class.",
  64.   "Set class implemented as hash table.",
  65.   "Stack class implemented as linked list.",
  66.   "Fully implemented string class.",
  67.   "Time of day class.",
  68.   "String tokenizer class." };
  69.  
  70. int main(int argc, char *argv[])                // Start main.
  71. {
  72.   CmBTreeDictionary classes;                    // Declare dictionary.
  73.  
  74.   for (int ii = 0; ii < TOTAL_CLASSES; ii++)    // Stuff with strings.
  75.     classes.add(new CmString(classnames[ii]), new CmString(descriptions[ii]));
  76.  
  77.   if (argc != 2)                                // If no argument,
  78.     printDictionary(classes);                   // print whole list.
  79.   else                                          // Otherwise, use argument
  80.   {                                             // as key and lookup.
  81.     CmString       key(argv[1]);
  82.     CmAssociation *found = (CmAssociation*) classes.lookupAssoc(&key);
  83.  
  84.     if (found)
  85.       cout << *(found->key()) << " - " << *(found->object()) << endl;
  86.     else
  87.       cout << "Class " << argv[1] << " not in dictionary." << endl;
  88.   }
  89.   return 0;
  90. }
  91.  
  92. void printDictionary(const CmBTreeDictionary& dict) // Print function.
  93. {
  94.   cout << "Compendium Container Class Library Object-Based Class List." << endl;
  95.   CmBTreeIterator iterator(dict);                   // Create an iterator.
  96.   while (iterator)                                  // While still alive,
  97.   {                                                 // print associations.
  98.     CmAssociation* pAssoc = (CmAssociation*) iterator++;
  99.     cout << *(pAssoc->key()) << " - " << *(pAssoc->object()) << endl;
  100.   }
  101. }
  102.